package com.kyzh.core.pager.weal.community

import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import com.gushenge.core.beans.FragCommunityBean
import com.kingja.loadsir.core.LoadSir
import com.kyzh.core.R
import com.kyzh.core.databinding.ViewSwipeRevBinding
import com.gushenge.core.base.fragment.BaseFragment
import com.kyzh.core.pager.weal.community.adapter.CommunityNewsAdapter
import com.kyzh.core.pager.weal.community.viewmodel.CommunityViewModel
import org.jetbrains.anko.support.v4.onRefresh
import per.goweii.layer.core.anim.AnimStyle
import per.goweii.layer.core.ktx.onClick
import per.goweii.layer.core.ktx.onClickToDismiss
import per.goweii.layer.core.widget.SwipeLayout
import per.goweii.layer.dialog.DialogLayer
import per.goweii.layer.dialog.ktx.*

class MyNewsFragment : BaseFragment() {
    private val vm  by viewModels<CommunityViewModel>()
    private lateinit var db : ViewSwipeRevBinding
    private var mp = 1
    private val adapter  = CommunityNewsAdapter()
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        db = ViewSwipeRevBinding.inflate(inflater)
        loadSir = LoadSir.getDefault().register(db.root) {
            reloadClick()
        }
        db.lifecycleOwner = this
        return loadSir.loadLayout
    }

    private fun reloadClick() {
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        db.rev.adapter = adapter
        adapter.setEmptyView(R.layout.empty)
        getData()
        adapter.setOnItemChildClickListener{a,v,p->
            when(v.id){
                R.id.delect->{
                    val data = a.data[p] as FragCommunityBean.Data
                    onBtnBottomSheetClick(data.id,p)
                }
            }
        }
        db.swipe.onRefresh {
            mp=1
            getData()
        }
        adapter.loadMoreModule.setOnLoadMoreListener {
            mp++
            vm.meNews(mp){
                loadSir.showSuccess()
                adapter.addData(this.data)
                adapter.loadMoreModule.loadMoreComplete()
                if(mp>=maxP){
                    adapter.loadMoreModule.loadMoreEnd(true)
                }
            }
        }
        adapter.setOnItemClickListener{a,v,p->
            val data = a.data[p] as FragCommunityBean.Data
            CommunityDetailActivity.start(requireActivity(),data.id,"")
        }
    }
    //删除内容的弹窗
  private   fun onBtnBottomSheetClick(id: String, p:Int) {
        DialogLayer(requireContext())
            .contentView(R.layout.dialog_delect_news)
            .gravity(Gravity.BOTTOM)
            .swipeDismiss(SwipeLayout.Direction.BOTTOM)
            .backgroundDimDefault()
            .animStyle(AnimStyle.BOTTOM)
            .onClickToDismiss(R.id.teCancel)
            .onClick(R.id.tvSure){
                vm.newsDel( id){
                    adapter.removeAt(p)
                }
                dismiss()
            }
            .show()
    }

    fun getData(){
        vm.meNews(mp){
            db.swipe.isRefreshing =false
            loadSir.showSuccess()
            adapter.setNewInstance(this.data)
            if(mp>=maxP){
                adapter.loadMoreModule.loadMoreEnd(true)
            }
        }
    }
}